﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using SMTC.Model.Domain;
using SMTC.BO.Plan;
using SMTC.WebCommon.Common;

public partial class PlanMangement_PlanUpLoad : BasePage
{
    private PlanProccessServices _services = new PlanProccessServices();
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btn_UploadByVersion_Click(object sender, EventArgs e)
    {
        DataSet ds = UP_PlanAnalyze.AnalyzeExcelToDS();
        string[] versionArray = dll_PlanVersion.Text.Split('-');
        PlanVersionDomain version = new PlanVersionDomain();
        version.VersionMonth = versionArray[0];
        version.VersionIndex = versionArray[1];
        version.VersionRemark = versionArray[2];

        List<ImportPlanDomain> list = this.GroupParse(this.ConvertPlanDSToList(ds));
        bool result = _services.InsertPlan(list,null,version);
        if (result == true)
        {
            ShowMessage("上传成功");
        }
        else
        {
            ShowMessage("上传失败");
        }
    }
    protected void btn_Upload_Click(object sender, EventArgs e)
    {
        DataSet ds = UP_PlanAnalyze.AnalyzeExcelToDS();
        string versionRemark = null;
        if (!string.IsNullOrEmpty(txt_VersionRemark.Text.Trim()))
        {
            versionRemark = txt_VersionRemark.Text.Trim();
        }
        List<ImportPlanDomain> list = this.GroupParse(this.ConvertPlanDSToList(ds));
        bool result = _services.InsertPlan(list, versionRemark);
        if (result == true)
        {
            ShowMessage("上传成功");
        }
        else
        {
            ShowMessage("上传失败");
        }
        ds.Tables.Clear();
        list.Clear();
    }

    /// <summary>
    /// DS Convert To List
    /// </summary>
    /// <param name="ds"></param>
    /// <returns></returns>
    protected List<ImportPlanDomain> ConvertPlanDSToList(DataSet ds)
    {
        int startRow = 0, buCol = ConvertDateType.IntParse(ConvertChartoNumber.ChartoNumber(txt_BuCol.Text.Trim()).ToString()) - 1
                        , materialCol = ConvertDateType.IntParse(ConvertChartoNumber.ChartoNumber(txt_ProductCol.Text.Trim()).ToString()) - 1
                        , planStartCol = ConvertDateType.IntParse(ConvertChartoNumber.ChartoNumber(txt_PlanStart.Text.Trim()).ToString()) - 1;
        List<ImportPlanDomain> list = new List<ImportPlanDomain>();

        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            if (string.IsNullOrEmpty(dr[buCol].ToString()))
            {
                continue;
            }
            else
            {
                ImportPlanDomain model = new ImportPlanDomain();
                model.MaterialCode = dr[materialCol].ToString();
                if (dr[materialCol].ToString() == "5WK49879")
                {
                    int i = 0;
                }
                model.BU = dr[buCol].ToString();
                model.m1 = ConvertDateType.IntParse(dr[planStartCol].ToString());
                model.m2 = ConvertDateType.IntParse(dr[planStartCol + 1].ToString());
                model.m3 = ConvertDateType.IntParse(dr[planStartCol + 2].ToString());
                model.m4 = ConvertDateType.IntParse(dr[planStartCol + 3].ToString());
                model.m5 = ConvertDateType.IntParse(dr[planStartCol + 4].ToString());
                model.m6 = ConvertDateType.IntParse(dr[planStartCol + 5].ToString());
                model.m7 = ConvertDateType.IntParse(dr[planStartCol + 6].ToString());
                model.m8 = ConvertDateType.IntParse(dr[planStartCol + 7].ToString());
                model.m9 = ConvertDateType.IntParse(dr[planStartCol + 8].ToString());
                model.m10 = ConvertDateType.IntParse(dr[planStartCol + 9].ToString());
                model.m11 = ConvertDateType.IntParse(dr[planStartCol + 10].ToString());
                model.m12 = ConvertDateType.IntParse(dr[planStartCol + 11].ToString());
                list.Add(model);
            }
        }

        return list;
    }
    /// <summary>
    /// 分组处理
    /// </summary>
    /// <param name="list"></param>
    /// <returns></returns>
    protected List<ImportPlanDomain> GroupParse(List<ImportPlanDomain> list)
    {
        var result = from item in list
                     group item by new { item.MaterialCode, item.BU } into g
                     select
                         new ImportPlanDomain
                         {
                             ID = Guid.NewGuid()
                             ,
                             MaterialCode = g.Key.MaterialCode
                             ,
                             BU = g.Key.BU
                             ,
                             m1 = g.Sum(a => a.m1)
                             ,
                             m2 = g.Sum(a => a.m2)
                             ,
                             m3 = g.Sum(a => a.m3)
                             ,
                             m4 = g.Sum(a => a.m4)
                             ,
                             m5 = g.Sum(a => a.m5)
                             ,
                             m6 = g.Sum(a => a.m6)
                             ,
                             m7 = g.Sum(a => a.m7)
                             ,
                             m8 = g.Sum(a => a.m8)
                             ,
                             m9 = g.Sum(a => a.m9)
                             ,
                             m10 = g.Sum(a => a.m10)
                             ,
                             m11 = g.Sum(a => a.m11)
                             ,
                             m12 = g.Sum(a => a.m12)
                             , Operation = this.UserName
                             , OperationTime = DateTime.Now
                         };
        return result.Cast<ImportPlanDomain>().ToList<ImportPlanDomain>();
    }
}